##  基础函数库
import numpy as np
import pandas as pd
## 导入LightGBM模型
from lightgbm.sklearn import LGBMClassifier
from sklearn import metrics
## 为了正确评估模型性能，将数据划分为训练集和测试集，并在训练集上训练模型，在测试集上验证模型性能。
from sklearn.model_selection import train_test_split

## 我们利用Pandas自带的read_csv函数读取并转化为DataFrame格式
df = pd.read_csv('../../doc/promotion/aicampml/happiness_train_complete.csv',encoding="unicode_escape")
df.fillna(0)
y = df.happiness
x = df.drop(["happiness","survey_time","edu_other","property_other","invest_other"],axis=1)
## 为了正确评估模型性能，将数据划分为训练集和测试集，并在训练集上训练模型，在测试集上验证模型性能。
data_target_part = y
data_features_part = x
## 测试集大小为20%， 80%/20%分
x_train, x_test, y_train, y_test = train_test_split(data_features_part, data_target_part, test_size = 0.1)
## 定义 LightGBM 模型
# ## 进行网格搜索获取的参数值
# clf = LGBMClassifier()
clf = LGBMClassifier(feature_fraction = 0.8,
                    learning_rate = 0.1,
                    max_depth= 8,
                    num_leaves = 16)
# 在训练集上训练LightGBM模型
clf.fit(x_train, y_train)
## 在训练集和测试集上分布利用训练好的模型进行预测
train_predict = clf.predict(x_train)
test_predict = clf.predict(x_test)
## 利用accuracy（准确度）【预测正确的样本数目占总预测样本数目的比例】评估模型效果
print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_train,train_predict))
print('The accuracy of the Logistic Regression is:',metrics.accuracy_score(y_test,test_predict))

dfTest = pd.read_csv('../../doc/promotion/aicampml/happiness_test_complete.csv',encoding="unicode_escape")
dfTest.fillna(-1)
testData = dfTest.drop(["survey_time","edu_other","property_other","invest_other"],axis=1)
test_result = clf.predict(testData)
#
dataframe = pd.DataFrame({"id":testData["id"].array,"happiness":test_result})
dataframe.to_csv("../../doc/promotion/aicampml/result.csv",index=False,sep=',')

if __name__ == '__main__':
    pass